package com.itheima.edu.info.maneger.dao;

import com.itheima.edu.info.maneger.domain.Student;

public class StudentDao implements BaseStudentDao{
    //1.创建Student学生数组长度为5
    private static Student[] stus = new Student[5];

    static {
        Student student = new Student("0001", "张三", 23, "1991");
        Student student2 = new Student("0002", "李四", 24, "1996");
        Student student3 = new Student("0003", "王五", 25, "1992");
        Student student4 = new Student("0004", "赵六", 23, "1994");

        stus[0] = student;
        stus[1] = student2;
        stus[2] = student3;
        stus[3] = student4;
    }
    public Student[] findAllStudent() {
        return stus;
    }


    //2.将接受到的学生对象添加到数组中
    public boolean addStudent(Student student) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Student stu = stus[i];
            if (stu == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //装满了
            return false;
        } else {
            //3.返回是否添加成功的boolean类型值
            stus[index] = student;
            return true;
        }

    }

    public void deleteStudent(String id) {
        int index = getIndex(id);
        stus[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }


    public void updateStudent(String updateId, Student newstudent) {
        int index = getIndex(updateId);
        stus[index] = newstudent;
    }
}













